package vip.meeet.algorithm.stack;


import java.util.Stack;


public class Num20 {

    public static void main(String[] args) {
        System.out.println(isValid("["));
        System.out.println(isValid("()"));
        System.out.println(isValid("()[]{}"));
        System.out.println(isValid("(]"));
        System.out.println(isValid("([)]"));
        System.out.println(isValid("{[]}"));

    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            if (currentChar == '{') {
                stack.push('}');
            } else if (currentChar == '(') {
                stack.push(')');
            } else if (currentChar == '[') {
                stack.push(']');
            } else {
                if (stack.empty()) {
                    return false;
                }
                if (currentChar != stack.pop()) {
                    return false;
                }
            }
        }
        return stack.empty();
    }


}
